﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FytSoa.Core.Model.Sw;
using FytSoa.Extensions;
using FytSoa.Service.DtoModel;
using FytSoa.Service.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FytSoa.Api.Controllers.Shop
{
    [Route("api/shop/[controller]")]    
    [ApiController]
    [JwtAuthorize(Roles = "Admin")]
    public class StoreController : ControllerBase
    {
        private readonly ISw_storeService _storeService;
        public StoreController(ISw_storeService storeService)
        {
            _storeService = storeService;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<IActionResult> Add([FromQuery]PageParm param)
        {
            return Ok(await _storeService.GetPagesAsync(param,m=>true,m=>m.Sort,Common.DbOrderEnum.Desc));
        }

        /// <summary>
        /// 根据id获得信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("getmodel")]
        public async Task<IActionResult> GetModel([FromBody]ParmString obj)
        {
            return Ok(await _storeService.GetModelAsync(m => m.Guid == obj.parm));
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<IActionResult> Add([FromBody]Sw_store model)
        {
            model.Guid = Guid.NewGuid().ToString();
            return Ok(await _storeService.AddAsync(model));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task<IActionResult> Update([FromBody]Sw_store model)
        {
            return Ok(await _storeService.UpdateAsync(model));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<IActionResult> Delete([FromBody]ParmString obj)
        {
            return Ok(await _storeService.DeleteAsync(obj.parm));
        }
    }
}